namespace OpenComposite.EII.Repository
{
	using System;
	using System.Collections.Generic;
	using System.Drawing;
	using System.Text;

	using OpenComposite.EII.Designer.SCA;

	using OpenComposite.Repository.Properties;

	[RepositoryItem(ItemType.SOBA, "sobas", "soba_id")]
	public class SOBA : RepositoryItemBaseEx
	{
		#region Initialization

		/// <summary>
		/// Initializes a new instance of the <see cref="SOBA"/> class.
		/// </summary>
		public SOBA()
			: base("soba_id", "soba_nm", "soba_desc", "soba_ntext", Resources.sNewSOBA)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="SOBA"/> class.
		/// </summary>
		/// <param name="bCreateNew">if set to <c>true</c> [b create new].</param>
		public SOBA(bool bCreateNew)
			: this()
		{
			if ( bCreateNew )
				this.CreateNew();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="SOBA"/> class.
		/// </summary>
		/// <param name="id">The id.</param>
		public SOBA(int id)
			: this()
		{
			this.Load(id);
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		/// <summary>
		/// Gets the allowed child types.
		/// </summary>
		/// <value>The allowed child types.</value>
		public override List<ItemType> AllowedChildTypes
		{
			get { return _LISTALLOWED; }
		}

		/// <summary>
		/// Gets the image.
		/// </summary>
		/// <value>The image.</value>
		public override Image Image
		{
			get { return Resources.diagram; }
		}

		/// <summary>
		/// Gets the image key.
		/// </summary>
		/// <value>The image key.</value>
		public override string ImageKey
		{
			get { return "diagram"; }
		}

		/// <summary>
		/// Gets the type of the repository item.
		/// </summary>
		/// <value>The type of the repository item.</value>
		public override ItemType RepositoryItemType
		{
			get { return ItemType.SOBA; }
		}

		/// <summary>
		/// Gets the repository item type description.
		/// </summary>
		/// <value>The repository item type description.</value>
		public override string RepositoryItemTypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the name of the repository item type.
		/// </summary>
		/// <value>The name of the repository item type.</value>
		public override string RepositoryItemTypeName
		{
			get { return Resources.sSOBA; }
		}

		/// <summary>
		/// Gets the selected image.
		/// </summary>
		/// <value>The selected image.</value>
		public override Image SelectedImage
		{
			get { return Resources.diagram; }
		}

		/// <summary>
		/// Gets the selected image key.
		/// </summary>
		/// <value>The selected image key.</value>
		public override string SelectedImageKey
		{
			get { return this.ImageKey; }
		}

		/// <summary>
		/// Gets or sets the symbol.
		/// </summary>
		/// <value>The symbol.</value>
		public SOBASymbol Symbol
		{
			get { return _symbol; }
			set { _symbol = value; }
		}

		/// <summary>
		/// Gets the name of the table.
		/// </summary>
		/// <value>The name of the table.</value>
		public override string TableName
		{
			get { return "sobas"; }
		}

		/// <summary>
		/// Gets or sets the workflow ID.
		/// </summary>
		/// <value>The workflow ID.</value>
		public int WorkflowID
		{
			get { return getInt("workflow_id", -1); }
			set { setValue("WorkflowID", "workflow_id", value); }
		}

		#endregion Properties

		#region Methods

		/// <summary>
		/// Copies this instance.
		/// </summary>
		/// <param name="deep"><c>true</c> to recursively copy the item and all subitems;
		/// <c>false</c> to copy only the item itself.</param>
		/// <returns>The copied item.</returns>
		public override IRepositoryItem Copy(bool deep)
		{
			SOBA copy = new SOBA(true);
			copy.InitializeServiceProvider(this);
			this.CopyDataRowToTarget(copy);
			return copy;
		}

		/// <summary>
		/// Gets the icon.
		/// </summary>
		/// <returns></returns>
		public override Icon GetIcon()
		{
			Bitmap bmp = new Bitmap(this.Image);
			bmp.MakeTransparent(Color.Magenta);
			return Icon.FromHandle(bmp.GetHicon());
		}

		/// <summary>
		/// Gets the workflow.
		/// </summary>
		/// <returns></returns>
		public Workflow GetWorkflow()
		{
			RepositoryService repSvc = (RepositoryService)this.GetService(typeof(RepositoryService));
			if ( repSvc == null )
				throw new NullReferenceException("RepositoryService not available.");

			if ( this.WorkflowID > 0 )
				return repSvc.GetItem(ItemType.Workflow, this.WorkflowID, false) as Workflow;
			else
				return null;
		}

		#endregion Methods

		#endregion Public Members

		#region Protected Members

		#region Properties

		/// <summary>
		/// Gets the delete question.
		/// </summary>
		/// <value>The delete question.</value>
		protected override string DeleteQuestion
		{
			get
			{
				string msg = base.DeleteQuestion;
				Workflow wf = this.GetWorkflow();
				if ( wf != null ) {
					switch ( wf.Type ) {
						case WorkflowType.Orchestration:
							msg = string.Format(
								"This will delete the SOBA \"{0}\" and the assigned Orchestration.{1}{1}" +
								"Do you want to continue?", wf.Name, Environment.NewLine);
							break;
						case WorkflowType.Business_Process:
							msg = string.Format(
								"This will delete the SOBA \"{0}\" and the assigned Business Process.{1}{1}" +
								"Do you want to continue?", wf.Name, Environment.NewLine);
							break;
						case WorkflowType.Mashup:
							msg = string.Format(
								"This will delete the SOBA \"{0}\" and the assigned Mashup.{1}{1}" +
								"Do you want to continue?", wf.Name, Environment.NewLine);
							break;
					}
				}
				return msg;
			}
		}

		#endregion Properties

		#region Methods

		/// <summary>
		/// Called when [item deleting].
		/// </summary>
		protected override void OnItemDeleting()
		{
			base.OnItemDeleting();
			Workflow wf = this.GetWorkflow();
			if ( wf != null )
				wf.DeleteInternal();
		}

		#endregion Methods

		#endregion Protected Members

		#region Private Members

		#region Static Fields

		private static List<ItemType> _LISTALLOWED = 
			new List<ItemType>(new ItemType[] { ItemType.GenericFolder, ItemType.Operation, ItemType.SCA_Export,
				ItemType.SCA_Import, ItemType.Component, ItemType.SCA_StandaloneReference, ItemType.Selector,
				ItemType.Composite });

		#endregion Static Fields

		#region Fields

		private SOBASymbol _symbol;

		#endregion Fields

		#endregion Private Members
	}
}